home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGBLER
/
WASM211B.LZH
/
BIN2DAT.ASM
next >
Wrap
Assembly Source File
|
1987-08-13
|
11KB
|
420 lines
Title 'Wolfware Assembler Sample Program', 'Binary to DATA Conversion'
;=============================================================================
; Binary to DATA Statement Conversion
;
; This program converts binary files into BASIC compatible DATA statements.
; This is useful for inserting external binary data, such as separately
; assembled machine language subroutines, into BASIC programs.
;
; The following example shows how to use a machine language subroutine
; stored in data statements. It is assumed that the size of the code is
; in SUBRTSIZE (the number of data components):
;
; 10 DEF SEG: OFFSET%=0 'BASIC data segment
; 20 SUB$ = STRING$(255,0) 'create string space
; 30 SUB = VARPTR(SUB$) 'string descriptor
; 40 SUB = PEEK(SUB+1) + (PEEK(SUB+2) * 256) 'string data location
; 50 FOR OFFSET%= 0 TO SUBRTSIZE-1 'for each byte
; 60 READ DAT% 'read a byte of data
; 70 POKE SUB + OFFSET%, DAT% 'insert into string
; 80 NEXT OFFSET% 'loop to get next byte
;
; Once this code has executed, the subroutine may be called through the
; variable SUB (see the sample program CLP.ASM). Note that the size of the
; code (defined above by SUFRTSIZE) is same as the size of the original
; binary file -- minus seven in the case of BSAVEd files.
;
; The files IO.MAC and IO.ASM are all needed for assembly. Make sure that
; IO.ASM is set up for non-library assembly. Note: this program does not
; use the runtime library IO.BIN, but rather includes the IO routines
; directly in the program.
Display Mes1 ;show opening message
Display Mes2 ;prompt
Input Namsiz ;get file name
Open ;open file
Pop Inphan ;store handle
Jc Main1 ;jump if error
Or Status, Open1 ;set flag
Display Mes3 ;prompt
Input Namsiz ;get file name
Create ;open file
Pop Linhan ;store handle
Jc Main2 ;jump if error
Or Status, Open2 ;set flag
Jmps Main3
;--- error, could not open input file
Main1
Mov Ax, Offset Err2
Jmp Criterr
;--- error, could not open output file
Main2
Mov Ax, Offset Err3
Jmp Criterr
;--- get execution parameters
Main3
Display Mes4 ;prompt
Input ;get response
Uppercase ;make upper-case
Display Al ;display
Line ;new line
Cmp Al, 'Y' ;check if yes
Jne Main12 ;jump if not
Or Status, Bload ;set flag
Main12
Display Mes5 ;prompt
Input ;get response
Uppercase ;make upper-case
Display Al ;display
Line ;new line
Cmp Al, 'Y' ;check if yes
Jne Main14 ;jump if not
Or Status, Lines ;set flag
Display Mes6 ;prompt
Input 5 ;get string
Jz Main17 ;jump if use default
Binary ;convert to binary
Pop Bx ;high word
Pop Ax ;low word
Jc Main13 ;jump if error, skip
Or Bx, Bx ;check if too big
Jnz Main13 ;jump if so, just skip
Mov Lin, Ax ;save starting number
Main17
Display Mes7 ;prompt
Input 5 ;get string
Jz Main14 ;jump if use default
Binary ;convert to binary
Pop Bx ;high word
Pop Ax ;low word
Jc Main13 ;jump if error, skip
Or Bx, Bx ;check if too big
Jnz Main13 ;jump if so, just skip
Mov Inc, Ax ;save increment
Jmps Main14
;--- illegal number
Main13
Mov Ax, Offset Err8
Jmp Criterr
;--- finish up initialization
Main14
Load Linbuf ;load line buffer location
Test Status, Bload ;check if bload
Jz Main4 ;jump if not
Mov Bx, Offset Inpbuf ;start of input buffer
Read Inphan, 7, Bx ;read bload header
Jc Main15 ;jump if error
Pop Ax ;bytes read
Add Sp, 2 ;throw out handle
Cmp Ax, 7 ;check if all bytes read
Jne Main16 ;jump if not, error
Cmp Byte [Bx], 0fdh ;check if BLOAD mark
Jne Main16 ;jump if not, error
Mov Ax, [Bx+5] ;get data size
Mov Bytcnt, Ax ;save
Jmps Main4
;--- error reading file
Main15
Mov Ax, Offset Err5
Jmp Criterr
;--- illegal bload format
Main16
Mov Ax, Offset Err7
Jmp Criterr
;--- loop for each line of data
Main4
Clear ;clear line
Test Status, Lines ;check if include line numbers
Jz Main5
Decimal Lin ;make line number decimal
Append ;add to line
Append ,,Str1 ;add space
Main5
Append ,,Str2 ;add data statment
Call Get_Byt ;get first byte
Jc Main10 ;jump if end of file
Mov Cx, Across ;set number to put across
Jmps Main7
;--- loop each value on the line
Main6
Push Cx
Call Get_Byt ;get next byte
Pop Cx
Jc Main9 ;jump if end of file
Append ,,Str3 ;add comma and space before previous number
Main7
Sub Ah, Ah
Decimal Ax ;convert to decimal string
Append ;add to line
Loop Main6 ;loop back if more values needed on line
;--- end of line
Append ,,Str4 ;add CR and LF
Cmp Cx, Across ;check if any data
Je Main8 ;jump if not, don't bother with empty DATA
Call Put_Lin ;write line
Main8
Mov Ax, Inc ;increment
Add Lin, Ax ;next line number
Jc Main11 ;jump if overflow
Jmps Main4
;--- end of file found, finished
Main9
Add Sp, 4 ;throw out line location
Cmp Cx, Across ;check if any data
Je Main10 ;jump if not, don't bother with empty DATA
Call Put_Lin ;write line
Main10
Line Mes8 ;display termination message
Call Cls_Fil ;close files
Mov Ax, 4c00h ;exit with error code 0
Int 21h
;--- line number overflow
Main11
Mov Ax, Offset Err4
;--- critical error entry
Criterr
Display Err1 ;display first part
Line Ax ;display message
Call Cls_Fil ;close files
Mov Ax, 4c01h ;exit with error code 1
Int 21h
;================================================
; The the next byte from the input file. The
; carry is set if end of file, otherwise AL
; returns the byte.
Get_Byt Proc Near
Mov Si, Inploc ;input location
Cmp Si, Inpend ;check if at end of data
Je Getbyt2
;--- not at end, get byte
Getbyt1
Lodsb ;get next byte
Mov Inploc, Si ;save pointer
Clc
Ret
;--- end of buffer reached, try to read more
Getbyt2
Mov Ax, Inpsiz ;get input buffer size (max bytes to read)
Test Status, Bload ;check if bload format
Jz Getbyt4 ;jump if so, read max bytes
Mov Bx, Bytcnt ;get remaining bytes to read
Or Bx, Bx ;check if done
Jz Getbyt6 ;jump if so
Cmp Ax, Bx ;check if still more to read
Jbe Getbyt3 ;jump if so
Mov Ax, Bx ;else set to remaining bytes
Getbyt3
Sub Bytcnt, Ax ;reduce count for bytes read
Getbyt4
Mov Si, Offset Inpbuf ;start of buffer
Read Inphan, Ax, Si ;read bytes
Jc Getbyt7 ;jump if error
Pop Bx ;get bytes read
Add Sp, 2 ;toss file handle
Test Status, Bload ;check if bload format
Jz Getbyt5
Cmp Bx, Ax ;using bload format, must have read all bytes
Jne Getbyt8 ;jump if not, error in bload file
Getbyt5
Or Bx, Bx ;check if no bytes read
Jz Getbyt6 ;jump if so
Add Bx, Si ;get end of buffer
Mov Inpend, Bx ;save
Jmps Getbyt1 ;jump back and get byte
;--- finished with input
Getbyt6
Stc
Ret
;--- error reading file
Getbyt7
Mov Ax, Offset Err5
Jmp Criterr
;--- error in BLOAD format
Getbyt8
Mov Ax, Offset Err7
Jmp Criterr
Endp ;Get_Byt
;================================================
; Write the present line to the output file.
Put_Lin Proc Near
Mov Si, Offset Linbuf ;location of string
Lodsb ;load length
Sub Ah, Ah
Write Linhan, Ax, Si ;write line
Jc Putlin1 ;jump if error
Pop Bx ;get bytes read
Add Sp, 2 ;toss file handle
Cmp Bx, Ax ;check if all bytes written
Jne Putlin1 ;jump if not, disk full
Ret
;--- write error or disk full
Putlin1
Mov Ax, Offset Err6
Jmp Criterr
Endp ;Put_Lin
;================================================
; Close the i/o files.
Cls_Fil Proc Near
Test Status, Open1 ;check if input is opened
Jz Clsfil1
Close Inphan ;close file
Clsfil1
Test Status, Open2 ;check if output is opened
Jz Clsfil2
Close Linhan ;close file
Clsfil2 Ret
Endp ;Cls_Fil
;================================================
; Program data.
Namsiz Equ 40 ;bytes allowed for file names
Linsiz Equ 79+1 ;line size (plus a byte for the string length)
Across Equ Linsiz-11/5 ;number of values to put on a single DATA line
Str1 Db 1,' ' ;single space
Str2 Db 5,'DATA ' ;DATA statement string
Str3 Db 2,', ' ;comma and space
Str4 Db 2,13,10 ;end of line marker
Lin Dw 10000 ;present (default) line number
Inc Dw 10 ;increment value
;--- program status
Bload Equ 01h ;bload format
Lines Equ 02h ;include line numbers
Open1 Equ 04h ;input file opened
Open2 Equ 04h ;output file opened
Status Db 0 ;default status
;--- initialized input data
Inpsiz Equ 2000 ;bytes in input buffer (must be at least 7)
Inploc Dw Offset Inpbuf ;present input location
Inpend Dw Offset Inpbuf ;end of input data
;--- program messages
Mes1 Label Byte
Db 155
Db 13,10
Db '*** Binary to DATA Statement Conversion ***',13,10
Db 13,10
Db 'This program converts a binary file to a list of data',13,10
Db 'statements that can be used in a BASIC program.',13,10
Db 13,10
Mes2 Db 30,'What file is to be converted? '
Mes3 Db 47,'What file should be used to store the results? '
Mes4 Db 40,13,10,'Do you wish to convert a BSAVEd file? '
Mes5 Db 50,'Should the BASIC statements include line numbers? '
Mes6 Db 49,'What should the starting line number be [10000]? '
Mes7 Db 47,'What should the line number increment be [10]? '
Mes8 Db 29,13,10,'File successfully converted'
;--- program termination messages
Err1 Db 11,13,10,'BIN2DAT: '
Err2 Db 25,'Could not open input file'
Err3 Db 26,'Could not open output file'
Err4 Db 25,'Line numbers out of range'
Err5 Db 10,'Read error'
Err6 Db 24,'Write error or disk full'
Err7 Db 21,'Error in BLOAD format'
Err8 Db 15,'Error in number'
;--- external source files
Include 'Io.Mac' ;i/o macro interface
Include 'Io.Asm' ;i/o routines
;--- unitialized data (must be at end of program)
Bytcnt Label Word ;byte count for BLOAD format files
Org +2
Inphan Label Word ;handle for input file
Org +2
Linhan Label Word ;handle for output file
Org +2
Inpbuf Label Byte ;input buffer
Org +Inpsiz
Linbuf Label Byte ;output (line) buffer
Org +Linsiz